package com.study.service.impl;

import com.study.entity.Dept;
import com.study.mapper.DeptMapper;
import com.study.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 业务逻辑处理层
 */
@Component("serviceBean") //程序启动时，会自动创建该类对象，并交由IOC容器管理,value可以指定bean对象名字，否则默认为类名首字母小写
                            //衍生注解：@Controller、@Service、@Repository
//声明bean要想生效，需要被扫描到，启动类默认扫描当前包及其子包（也可以指定包@ComponentScan("packAgeName"）
public class DeptServiceImpl implements DeptService {
    //    private DeptDao deptDao = new DeptDaoImpl();
    @Autowired //从IOC容器中，自动寻找bean对象，为该变量赋值
    private DeptMapper deptMapper;

    public List<Dept> list() {
        List<Dept> depts = deptMapper.depts();
        for (Dept dept : depts) {
            System.out.println(dept);
        }
        //3、返回封装好的数据
        return depts;
    }

    @Override
    public void delete(Integer id) {
        deptMapper.delete(id);
    }

    @Override
    public void add(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.add(dept);    }

    @Override
    public Dept selectBy(Integer id) {
       Dept dept =  deptMapper.selectById(id);
       return dept;
    }

    @Override
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }
}
